import common from '../common.js'
import invokeTable from './invokeTable.js'
import invokeSearch from './invokeSearch.js'
import SearchHeader from '../../components/table/SearchHeader'
import MultiFunTable from '../../components/table/MultiFunTable'
import TablePagination from '../../components/table/TablePagination'

export default {
  mixins: [invokeTable, invokeSearch],
  components: {
    SearchHeader,
    MultiFunTable,
    TablePagination
  },
  data () {
    return {
      searchParams: {},
      pageNumber: 1,
      pageSize: 10,
      columnName: '', //把列名经过处理转成数据库需要的格式
      sortType: '',
      notFormatColumn: '',
      selectionList: ''
    }
  },
  computed: {
    // 计算当前页面是否有打印按钮
    isNeedPrint: function(){
      let funList = this.functionBtnList;
      let _idx = funList.findByKey('functionName', 'bt_print');
      if(_idx > -1) {
        return true;
      } else {
        return false;
      }
    }
  },
  watch : {
    notFormatColumn: function (val) {
      this.columnName = this.handleOrderBy(val);
    }
  },
  created () {
    let params = {
      userToken: this.userToken,
      tableName: this.$route.meta
    };
    this.$store.dispatch('getUserSetting', params);
    this.getSortObj();
  },
  methods: {
    confirmSearch(params) {
      this.pageNumber = 1;
      this.getList();
    },
    getSortObj () {
      let meta = this.$route.meta;
      let info = localStorage.getItem(meta);
      info = info ? JSON.parse(info) : {};
      if(info && info.sort) {
        let sortObj = info.sort;
        let _orderTy = sortObj.orderTy;
        this.notFormatColumn = sortObj.orderBy;
        this.sortType = _orderTy == '' ? '' : (_orderTy == 'ascending' ? 'ASC' : 'DESC');
        this.columnName = this.handleOrderBy(sortObj.orderBy);
      }
    },
    // 设置默认排序列
    setDefaultSortCol (sort, order) {
      this.notFormatColumn = sort;
      this.sortType = order;
    },
    //将排序的字段名称(驼峰式)转为后台数据库所需要的格式（下划线分隔式） 
    // eg. parkingSpaceNumber 转为 parking_space_number
    handleOrderBy (prop) {
      if(prop) {
        if('Str' === prop.substr(-3)) {
          prop = prop.substring(0, prop.length - 3);
        }
        return prop.split('').map(x => {
          return x >= 'A' && x <='Z' ? '_' + x.toLowerCase() : x;
        }).reduce((x, y) => {
          return x + y;
        })
      }
      return '';
    },
    getPagination (curPage, pageSize) {
      this.pageNumber = curPage;
      this.pageSize = pageSize ? pageSize : this.pageSize;
      this.getList();
    },
    getNextPage (flag) {
      let pages = Math.ceil(this.total / this.pageSize);
      switch(flag) {
        case '+': 
          if(this.pageNumber >= pages) {
            this.showToast('当前已经是最后一页！', 2, 1500);
            return;
          } else {
            this.pageNumber++;
          }
          break;
        case '-': 
          if(this.pageNumber <= 1) {
            this.showToast('当前已经是第一页！', 2, 1500);
            return;
          } else {
            this.pageNumber--;
          }
          break;
        default: break;
      }
      this.getList();
    }
  }
}